package com.yanqu.road.dao.impl.activity.zhenhuan;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.CrossZhenHuanPartyData;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.CrossZhenHuanPartyJoinData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossZhenHuanPartyJoinImpl extends TempDataObjectDao<CrossZhenHuanPartyJoinData> {

    public static String getCrossZhenHuanPartyJoinDataSql = "select * from `t_c_zhenhuan_party_join` where `activityId`=?";
    public List<CrossZhenHuanPartyJoinData> getCrossZhenHuanPartyJoinData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossZhenHuanPartyJoinData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossZhenHuanPartyJoinDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossZhenHuanPartyJoinData data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String updateCrossZhenHuanPartyJoinDataSql = "update `t_c_zhenhuan_party_join` set userId=?,userLevel=?,userPrestige=?,joinTime=?,matchPrestige=?,isNpc=? where activityId=? and partyId=? and pos=?";
    @Override
    protected boolean updateData(CrossZhenHuanPartyJoinData crossZhenHuanPartyJoinData) {
        crossZhenHuanPartyJoinData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyJoinData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyJoinData.getUserLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyJoinData.getUserPrestige()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyJoinData.getJoinTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyJoinData.getMatchPrestige()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyJoinData.getIsNpc()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyJoinData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyJoinData.getPartyId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyJoinData.getPos()));
        if (getDb().executeNoneQuery(updateCrossZhenHuanPartyJoinDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossZhenHuanPartyJoinDataSql = "insert into `t_c_zhenhuan_party_join`(activityId,partyId,pos,userId,userLevel,userPrestige,joinTime,matchPrestige,isNpc) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossZhenHuanPartyJoinData crossZhenHuanPartyJoinData) {
        crossZhenHuanPartyJoinData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyJoinData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyJoinData.getPartyId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyJoinData.getPos()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyJoinData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyJoinData.getUserLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyJoinData.getUserPrestige()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyJoinData.getJoinTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPartyJoinData.getMatchPrestige()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPartyJoinData.getIsNpc()));
        if (getDb().executeNoneQuery(insertCrossZhenHuanPartyJoinDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossZhenHuanPartyJoinData parseData(ResultSet rs) throws SQLException {
        CrossZhenHuanPartyJoinData crossZhenHuanPartyJoinData = new CrossZhenHuanPartyJoinData();
        crossZhenHuanPartyJoinData.setActivityId(rs.getInt("activityId"));
        crossZhenHuanPartyJoinData.setPartyId(rs.getLong("partyId"));
        crossZhenHuanPartyJoinData.setPos(rs.getInt("pos"));
        crossZhenHuanPartyJoinData.setUserId(rs.getLong("userId"));
        crossZhenHuanPartyJoinData.setUserLevel(rs.getInt("userLevel"));
        crossZhenHuanPartyJoinData.setUserPrestige(rs.getLong("userPrestige"));
        crossZhenHuanPartyJoinData.setJoinTime(rs.getLong("joinTime"));
        crossZhenHuanPartyJoinData.setMatchPrestige(rs.getLong("matchPrestige"));
        crossZhenHuanPartyJoinData.setIsNpc(rs.getInt("isNpc"));

        return crossZhenHuanPartyJoinData;
    }
}
